package com.sgb.kjwl.view.ui.adapter.machine;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ItemMyAskLeaseBinding;
import com.sgb.kjwl.model.entity.self.LeaseAskStateEntity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseFactoryActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseFarmActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseHouseActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseLandActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.PublishLeaseInfoActivity2;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyAskLeaseAdapter extends BaseRecycleAdapter2<LeaseAskStateEntity> {

    private OnCallBlackListener listener;

    public MyAskLeaseAdapter(Context context, List<LeaseAskStateEntity> datas) {
        super(context, R.layout.item_my_ask_lease, datas);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, LeaseAskStateEntity entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        ItemMyAskLeaseBinding binding = (ItemMyAskLeaseBinding) holder.getmBinding();
        try {
            if (!TextUtils.isEmpty(entity.getTaskAddr())) {
                binding.tvLeaseAddr.setText(entity.getTaskAddr());
            } else {
                binding.tvLeaseAddr.setText(entity.getAddress());
            }

            binding.tvLeasePublicTime.setText(DateUtils.stampToDay(entity.getUpdateTime()));
            if (!TextUtils.isEmpty(entity.getRentalDuration())) {
                binding.tvLeaseTime.setText(entity.getRentalDuration() + entity.getRentalDurationUnit());
                if ("面议".equals(entity.getRentalDuration().trim()) || "-1".equals(entity.getRentalDuration().trim())) {
                    binding.tvLeaseTime.setText("面议");
                }
            }
            if (!TextUtils.isEmpty(entity.getNumOrArea())) {
                binding.tvNum.setText(entity.getNumOrArea());
            }else{
                binding.tvNum.setText("暂无信息");
            }
            if (!TextUtils.isEmpty(entity.getPrice())) {
                binding.tvSalary.setText(entity.getPrice());
            }else{
                binding.tvNum.setText("暂无信息");
            }
            //设置土地租赁面积
            if ("3".equals(entity.getType()) || "4".equals(entity.getType()) || "5".equals(entity.getType())) {
                binding.tvNumLabel.setText("租赁面积");
            } else {
                binding.tvNumLabel.setText("租赁数量");
            }
        } catch (Exception ex) {

        }

        //状态(1:报价中;2:流标;3:报价结束;4:已完成)
        if ("1".equals(entity.getStatus())) {
            binding.ivStatus.setImageResource(R.mipmap.ic_ask_lease_qute);
            if ("0".equals(entity.getIsEdit())) {
                binding.tvStateEdit.setVisibility(View.VISIBLE);
//                binding.tvStateDelete.setVisibility(View.VISIBLE);
            } else {
                binding.tvStateEdit.setVisibility(View.GONE);
//                binding.tvStateDelete.setVisibility(View.GONE);
            }
        } else if ("3".equals(entity.getStatus())) {
            binding.ivStatus.setImageResource(R.mipmap.ic_ask_lease_over);
            binding.tvStateEdit.setVisibility(View.GONE);
//            if (entity2.getQuotePersonCount() <= 0) {
//                binding.tvStateDelete.setVisibility(View.VISIBLE);
//            } else {
//                binding.tvStateDelete.setVisibility(View.GONE);
//            }
        } else if ("4".equals(entity.getStatus())) {
            binding.ivStatus.setImageResource(R.mipmap.ic_ask_lease_done);
            binding.tvStateEdit.setVisibility(View.GONE);
//            if (entity2.getCanOperate() == 1) {//已完成状态下,是否显示删除按钮
//                binding.tvStateDelete.setVisibility(View.GONE);
//            } else {
//                binding.tvStateDelete.setVisibility(View.VISIBLE);
//            }
        } else {
            binding.ivStatus.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(v -> listener.onDetail(entity.getType(), entity.getNo()));
        binding.tvStateDelete.setOnClickListener(v -> showDialog(entity.getNo(), entity.getUpdateTime(), entity.getType()));
        binding.tvStateEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("3".equals(entity.getType())) {//房产
                    AskLeaseHouseActivity2.start(context, entity.getNo());
                } else if ("4".equals(entity.getType())) {//土地
                    AskLeaseLandActivity2.start(context, entity.getNo());
                } else if ("5".equals(entity.getType())) {//工厂
                    AskLeaseFactoryActivity2.start(context, entity.getNo());
                } else if ("6".equals(entity.getType())) {//农机
                    AskLeaseFarmActivity2.start(context, entity.getNo());
                } else {
                    PublishLeaseInfoActivity2.start(context, Integer.parseInt(entity.getType()), entity.getNo());
                }

            }
        });

    }

    public interface OnCallBlackListener {
        void onRefresh();

        void onDetail(String categoryType, String id);
    }

    public void setOnCallBlackListener(OnCallBlackListener listener) {
        this.listener = listener;
    }

//    public void getH5Url(int position, String id) {
//        String urlName = "";
//        if (position == 0) {
//            urlName = "mechanicsoffer";
//        } else if (position == 1) {
//            urlName = "equipmentoffer";
//        } else if (position == 2) {
//            urlName = "vehicleoffer";
//        } else if (position == 3) {
//            urlName = "houseoffer";
//        } else if (position == 4) {
//            urlName = "landOfferDetails";
//        } else if (position == 5) {
//            urlName = "factoryOfferDetails";
//        } else if (position == 6) {
//            urlName = "farmOfferDetails";
//        }
//        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    String url = response.body().getData().getUrl() + "?id=" + id;
////                    String url = "http://192.168.2.103:8068/personCenterMechanicalDetail" + "?id=" + id;
//                    WebViewX5HostActivity.start(context, url);
//                } else {
//                    MToast.showToast(context, response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }

    private void delete(String id, String time, String categoryType) {
        DialogHelper.showProgressDialog(context, "提交中", 0, false);
        Map map = new HashMap();
        map.put("rentalSeekId", id);
        map.put("updateTime", time);
        map.put("categoryType", categoryType);
        APIManager.getInstance().getMachineAPI().deteleMatch(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(context, "删除成功");
                    if (null != listener) {
                        listener.onRefresh();
                    }
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //确认删除对话框
    public void showDialog(String id, String time, String categoryType) {
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_message.setText("是否确认删除?");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(id, time, categoryType);
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
